shortcutcontroller: Introduce
authorBenjamin Otte <otte@redhat.com>
Mon, 30 Jul 2018 04:02:20 +0000 (06:02 +0200)
committerMatthias Clasen <mclasen@redhat.com>
Thu, 26 Mar 2020 02:36:03 +0000 (22:36 -0400)
This is a very barebones controller that currently does nothing but
activate the binding signals. Yay.

And because we have bindings on every widget (Yes, a GtkGrid has a
keybinding - 2 in fact), we need that controller everywhere.

docs/reference/gtk/gtk4-docs.xml
docs/reference/gtk/gtk4-sections.txt
docs/reference/gtk/gtk4.types.in
gtk/gtk.h
gtk/gtkshortcutcontroller.c [new file with mode: 0644]
gtk/gtkshortcutcontroller.h [new file with mode: 0644]
gtk/gtkwidget.c
gtk/meson.build

index aabc016df4b4c56073193fb0737b790d88dff132..b66170816b66f81c33e558577601c87b9b3f431f 100644 (file)
       <xi:include href="xml/gtkgesturezoom.xml" />
       <xi:include href="xml/gtkgesturestylus.xml" />
       <xi:include href="xml/gtkpadcontroller.xml" />
+      <xi:include href="xml/gtkshortcutcontroller.xml" />
     </chapter>
 
     <chapter>
index 77810a810ca9b1d30e0ae9442472e8d220d807fc..f0fa4d3cc6a24894ec2badfca91de535b90b23da 100644 (file)
@@ -6063,6 +6063,24 @@ GTK_EVENT_CONTROLLER_MOTION_GET_CLASS
 gtk_event_controller_motion_get_type
 </SECTION>
 
+<SECTION>
+<FILE>gtkshortcutcontroller</FILE>
+<TITLE>GtkShortcutController</TITLE>
+GtkShortcutController
+gtk_shortcut_controller_new
+
+<SUBSECTION Standard>
+GTK_TYPE_SHORTCUT_CONTROLLER
+GTK_SHORTCUT_CONTROLLER
+GTK_SHORTCUT_CONTROLLER_CLASS
+GTK_IS_SHORTCUT_CONTROLLER
+GTK_IS_SHORTCUT_CONTROLLER_CLASS
+GTK_SHORTCUT_CONTROLLER_GET_CLASS
+
+<SUBSECTION Private>
+gtk_shortcut_controller_get_type
+</SECTION>
+
 <SECTION>
 <FILE>gtkeventcontrollerkey</FILE>
 <TITLE>GtkEventControllerKey</TITLE>
index 1c7b9eabb207c251061cab328cb043ed3db8e2af..d3d45fb0136643019fabb72ea5f2bd0a6069dfd2 100644 (file)
@@ -165,6 +165,7 @@ gtk_search_entry_get_type
 gtk_selection_model_get_type
 gtk_separator_get_type
 gtk_settings_get_type
+gtk_shortcut_controller_get_type
 gtk_shortcut_label_get_type
 gtk_shortcuts_window_get_type
 gtk_shortcuts_section_get_type
index 603fe9f9fd6e172cebd1401d52d5b412d5b97988..4304d6888a671b32dc8d90c4a476008cd0eae644 100644 (file)
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
 #include <gtk/gtkselectionmodel.h>
 #include <gtk/gtkseparator.h>
 #include <gtk/gtksettings.h>
+#include <gtk/gtkshortcutcontroller.h>
 #include <gtk/gtkshortcutlabel.h>
 #include <gtk/gtkshortcutsgroup.h>
 #include <gtk/gtkshortcutssection.h>
diff --git a/gtk/gtkshortcutcontroller.c b/gtk/gtkshortcutcontroller.c
new file mode 100644 (file)
index 0000000..58bd4c0
--- /dev/null
@@ -0,0 +1,96 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+
+/**
+ * SECTION:gtkshortcutcontroller
+ * @Short_description: Event controller for shortcuts
+ * @Title: GtkShortcutController
+ * @See_also: #GtkEventController, #GtkShortcut
+ *
+ * #GtkShortcutController is an event controller that manages shortcuts.
+ **/
+
+#include "config.h"
+
+#include "gtkshortcutcontroller.h"
+
+#include "gtkeventcontrollerprivate.h"
+#include "gtkbindings.h"
+
+#include <gdk/gdk.h>
+
+struct _GtkShortcutController
+{
+  GtkEventController parent_instance;
+};
+
+struct _GtkShortcutControllerClass
+{
+  GtkEventControllerClass parent_class;
+};
+
+G_DEFINE_TYPE (GtkShortcutController, gtk_shortcut_controller,
+               GTK_TYPE_EVENT_CONTROLLER)
+
+static void
+gtk_shortcut_controller_finalize (GObject *object)
+{
+  //GtkShortcutController *self = GTK_SHORTCUT_CONTROLLER (object);
+
+  G_OBJECT_CLASS (gtk_shortcut_controller_parent_class)->finalize (object);
+}
+
+static gboolean
+gtk_shortcut_controller_handle_event (GtkEventController *controller,
+                                      GdkEvent           *event,
+                                      double              x,
+                                      double              y)
+{
+  GdkEventType event_type = gdk_event_get_event_type (event);
+
+  if (event_type == GDK_KEY_PRESS ||
+      event_type == GDK_KEY_RELEASE)
+    return gtk_bindings_activate_event (G_OBJECT (gtk_event_controller_get_widget (controller)),
+                                        event);
+
+  return FALSE;
+}
+
+static void
+gtk_shortcut_controller_class_init (GtkShortcutControllerClass *klass)
+{
+  GtkEventControllerClass *controller_class = GTK_EVENT_CONTROLLER_CLASS (klass);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gtk_shortcut_controller_finalize;
+  controller_class->handle_event = gtk_shortcut_controller_handle_event;
+}
+
+static void
+gtk_shortcut_controller_init (GtkShortcutController *controller)
+{
+}
+
+GtkEventController *
+gtk_shortcut_controller_new (void)
+{
+  return g_object_new (GTK_TYPE_SHORTCUT_CONTROLLER,
+                       NULL);
+}
diff --git a/gtk/gtkshortcutcontroller.h b/gtk/gtkshortcutcontroller.h
new file mode 100644 (file)
index 0000000..91fb97d
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright © 2018 Benjamin Otte
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Benjamin Otte <otte@gnome.org>
+ */
+
+
+#ifndef __GTK_SHORTCUT_CONTROLLER_H__
+#define __GTK_SHORTCUT_CONTROLLER_H__
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtkeventcontroller.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_SHORTCUT_CONTROLLER         (gtk_shortcut_controller_get_type ())
+#define GTK_SHORTCUT_CONTROLLER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GTK_TYPE_SHORTCUT_CONTROLLER, GtkShortcutController))
+#define GTK_SHORTCUT_CONTROLLER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST ((k), GTK_TYPE_SHORTCUT_CONTROLLER, GtkShortcutControllerClass))
+#define GTK_IS_SHORTCUT_CONTROLLER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GTK_TYPE_SHORTCUT_CONTROLLER))
+#define GTK_IS_SHORTCUT_CONTROLLER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GTK_TYPE_SHORTCUT_CONTROLLER))
+#define GTK_SHORTCUT_CONTROLLER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GTK_TYPE_SHORTCUT_CONTROLLER, GtkShortcutControllerClass))
+
+typedef struct _GtkShortcutController GtkShortcutController;
+typedef struct _GtkShortcutControllerClass GtkShortcutControllerClass;
+
+GDK_AVAILABLE_IN_ALL
+GType                   gtk_shortcut_controller_get_type                (void) G_GNUC_CONST;
+
+GDK_AVAILABLE_IN_ALL
+GtkEventController *    gtk_shortcut_controller_new                     (void);
+
+G_END_DECLS
+
+#endif /* __GTK_SHORTCUT_CONTROLLER_H__ */
index 48ead879f06e35e2b4f4841742e9fff7579be217..b077180920748da7e088473c009d7996b7287847 100644 (file)
@@ -59,6 +59,7 @@
 #include "gtknativeprivate.h"
 #include "gtkscrollable.h"
 #include "gtksettingsprivate.h"
+#include "gtkshortcutcontroller.h"
 #include "gtksizegroup-private.h"
 #include "gtksnapshotprivate.h"
 #include "gtkstylecontextprivate.h"
@@ -2332,6 +2333,7 @@ gtk_widget_init (GTypeInstance *instance, gpointer g_class)
   GtkWidget *widget = GTK_WIDGET (instance);
   GtkWidgetPrivate *priv = gtk_widget_get_instance_private (widget);
   GType layout_manager_type;
+  GtkEventController *controller;
 
   widget->priv = priv;
 
@@ -2403,6 +2405,10 @@ gtk_widget_init (GTypeInstance *instance, gpointer g_class)
   layout_manager_type = gtk_widget_class_get_layout_manager_type (g_class);
   if (layout_manager_type != G_TYPE_INVALID)
     gtk_widget_set_layout_manager (widget, g_object_new (layout_manager_type, NULL));
+
+  controller = gtk_shortcut_controller_new ();
+  gtk_event_controller_set_name (controller, "gtk-widget-class-shortcuts");
+  gtk_widget_add_controller (widget, controller);
 }
 
 /**
@@ -4894,11 +4900,6 @@ gtk_widget_event (GtkWidget *widget,
   if (return_val == FALSE)
     return_val |= gtk_widget_run_controllers (widget, event, target, x, y, GTK_PHASE_BUBBLE);
 
-  if (return_val == FALSE &&
-      (gdk_event_get_event_type (event) == GDK_KEY_PRESS ||
-       gdk_event_get_event_type (event) == GDK_KEY_RELEASE))
-    return_val |= gtk_bindings_activate_event (G_OBJECT (widget), event);
-
   return return_val;
 }
 
index bf7e08a06ce0ecd1709d3141178e23eaba85833b..b58180f032171d339640772218dcd20d661b5ff4 100644 (file)
@@ -331,6 +331,7 @@ gtk_public_sources = files([
   'gtkselectionmodel.c',
   'gtkseparator.c',
   'gtksettings.c',
+  'gtkshortcutcontroller.c',
   'gtkshortcutlabel.c',
   'gtkshortcutsgroup.c',
   'gtkshortcutssection.c',
@@ -568,6 +569,7 @@ gtk_public_headers = files([
   'gtkselectionmodel.h',
   'gtkseparator.h',
   'gtksettings.h',
+  'gtkshortcutcontroller.h',
   'gtkshortcutlabel.h',
   'gtkshortcutsgroup.h',
   'gtkshortcutssection.h',